<template>
  <div class="mod-blog__category">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:params:save')" type="primary"
          @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <TableField :config="config" ref="tableRef">
      <el-table-column width="55" fixed="left" align="center">
      </el-table-column>
      <el-table-column label="分类名" prop="name"></el-table-column>
      <el-table-column label="排序" prop="sort">
        <template #default="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="170">
        <template #default="scope">
          <el-button type="primary" v-if="scope.row.id != 0 && scope.row.id != 1" link @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button type="primary" v-if="scope.row.id != 0 && scope.row.id != 1" link @click="deleteItem(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </TableField>
    <ChyDrawer v-model="isShowDrawer" :title="dataForm.title" @confirm="dataFormSubmitHandle()">
      <el-form :model="dataForm" class="data_form" ref="dataFormRef" :rules="rules"
        @keyup.enter="dataFormSubmitHandle()" label-width="auto" style="max-width: 600px">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="dataForm.name" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="dataForm.sort" :min="1" :max="999" />
        </el-form-item>
      </el-form>
    </ChyDrawer>
  </div>
</template>

<script setup lang='ts'>
import useView from '@/hooks/useView'
import { ref, reactive, toRefs, onMounted } from 'vue'
import TableField from '@/components/table-field'
import ChyDrawer from "@/components/chy-drawer"
import http from "@/utils/http"
import { ElMessage } from "element-plus"
import { addBlogCategory, updateBlogCategory } from "@/api/blogCategory"

const view = reactive({
  deleteURL: "blogCategory"
})
const state = reactive({ ...useView(view), ...toRefs(view) })

let config = ref({
  url: 'blogCategory',
  rootProperty: 'list',
  method: 'get',
  data: {
  }
})

const dataFormRef = ref()
const tableRef = ref()

const isShowDrawer = ref(false)

type DataType = {
  id?: number,
  title: string,
  name: string,
  sort: number,
}
const dataForm = reactive<DataType>({
  id: 0,
  title: '',
  name: '',
  sort: 0
})

const rules = ref({
  name: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  sort: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
})

onMounted(() => {
  if (tableRef.value) {
    tableRef.value.loadData()
  }
})
const addOrUpdateHandle = (row?: any) => {
  isShowDrawer.value = true
  if (!row) {
    dataForm.title = '新增'
    dataForm.name = ''
    dataForm.sort = 0
    delete dataForm.id
  } else {
    dataForm.title = '修改'
    dataForm.id = row.id
    dataForm.name = row.name
    dataForm.sort = row.sort
  }
}

const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }
    (!dataForm.id ? addBlogCategory : updateBlogCategory)({ ...dataForm }).then(res => {
      if (res.code === 0) {
        ElMessage.success({
          message: "成功",
          duration: 500,
          onClose: () => {
            isShowDrawer.value = false
            tableRef.value.loadData()
          }
        })
      }
    })
  })

}

const deleteItem = (id: string) => {
  state.deleteHandle(id).then(res => {
    if (res) {
      tableRef.value.loadData()
    }
  })
}

</script>

<style lang='scss' scoped></style>